/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.web.generator.propery;

import com.farmcode.console.api.business.PropertyManager;
import com.farmcode.console.api.model.Property;
import com.farmcode.console.api.util.Constants;
import com.farmcode.web.generator.BaseCodeGenerator;
import com.farmcode.web.generator.CodeGeneratorBackingBean;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

/**
 *
 * @author Oscar Ivan Prado
 */
@Named(value = "variableBackingBean")
@RequestScoped
public class VariableBackingBean extends BaseProperty{

    private Property newProperty;
    
    /** Creates a new instance of VariableBackingBean */
    public VariableBackingBean() {
    }

    public void onCreate(){
        Property property = getNewProperty();
        property.setType(Constants.VARIABLE);
        property.setProject(getProject());
        propertyManager.create(property);
        setNewProperty(new Property());
        getSession().setValue(new CodeGeneratorBackingBean().getClass().getCanonicalName() +".variables", null);
    }
    
    public void onRemove(Property property){
        propertyManager.remove(property);
        getSession().setValue(new CodeGeneratorBackingBean().getClass().getCanonicalName() +".variables", null);
    }
    
    public void onModfy(Property property){
        propertyManager.store(property);
        
    }
    
    /**
     * @return the property
     */
    public Property getNewProperty() {
        newProperty = new Property();
        if(getSession().getValue(newProperty.getClass().getCanonicalName() +".newProperty") != null){
            newProperty = getSession().getValue(newProperty.getClass().getCanonicalName() +".newProperty");
        }else{
            setNewProperty(newProperty);
        }
        return newProperty;
    }

    /**
     * @param property the property to set
     */
    public void setNewProperty(Property property) {
        this.newProperty = property;
        getSession().setValue(property.getClass().getCanonicalName() +".newProperty", newProperty);
    }
}
